package com.zxd.threads.thread_pool.thread_pool_util;

import com.zxd.threads.thread_pool.runnable_task.MyRunnable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName:Test
 * Package:com.zxd.threads.thread_pool.thread_pool_util
 *
 * @Date:2023/9/26 22:43
 * @Author:zxde
 */
public class Test {
    public static void main(String[] args) {
        //使用Executors工具类创建的ExecutorService对象，其任务队列使用的是链表，其长度是无穷大的，所以大型并发系统可能会造成系统的瘫痪
        ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(3);
        //创建一个线程的线程池，线程出现异常再创建线程
//        ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
        //随着任务增加而新增线程数量
//        ExecutorService threadPoolExecutor = Executors.newCachedThreadPool();
        //核心线程处理
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        //放入任务队列
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        //临时线程处理
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        threadPoolExecutor.execute(new com.zxd.threads.thread_pool.runnable_task.MyRunnable());
        //RejectedExecutionHandler
        /** 在ThreadPoolExecutor中存在四种静态内部类；
         * 其都实现了RejectedExecutionHandler接口，可以作为ThreadPoolExecutor构造器的最后一个参数
         * 使用new ThreadPoolExecutor.CallerRunsPolicy(),即可返回相应的对象
         *          CallerRunsPolicy        超出线程由main线程执行
         *          DiscardOldestPolicy     遗弃存在时间最长的线程超出线程
         *          DiscardPolicy           丢弃超出线程
         *          AbortPolicy             不执行，直接报错
         */
        threadPoolExecutor.execute(new MyRunnable());
    }
}
